package com.imau.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imau.bean.SysDept;
import com.imau.common.utils.SqlSessionFactoryUtil;
import com.imau.common.utils.StringLike;
import com.imau.mapper.SysDeptMapper;
import com.imau.service.SysdeptService;
import io.mybatis.mapper.example.Example;
import io.mybatis.service.AbstractService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class SysdeptServiceImpl extends AbstractService<SysDept, String, SysDeptMapper> implements SysdeptService {


    @Override
    public SysDept save(SysDept dept) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);

        // 设置创建人
       // dept.setCreateBy();
        SysDept sysDept = baseMapper.selectByPrimaryKey(dept.getParentId()).get();
        dept.setAncestors(sysDept.getAncestors()+","+sysDept.getDid());
        dept.setCreateTime(new Date());
        
        SysDept save = super.save(dept);
        session.close();

        return save;

    }

    @Override
    public int deleteByDid(String did) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);


        int i = baseMapper.deleteByPrimaryKey(did);

        session.close();

        return i;
    }

    @Override
    public int updateDept(SysDept dept) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);

        // 设置创建人
        // dept.setCreateBy();
        SysDept newParentDept = baseMapper.selectByPrimaryKey(dept.getParentId()).get();
        SysDept oldDept = baseMapper.selectByPrimaryKey(dept.getDid()).get();

        if (newParentDept!=null && oldDept!=null)
        {
            String newAncestors =newParentDept.getAncestors()+","+newParentDept.getDid();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getDid(), newAncestors, oldAncestors);
        }



        dept.setUpdateTime(new Date());

        int i = baseMapper.updateByPrimaryKey(dept);

        session.close();
        return i;
    }

    @Override
    public SysDept findById(String did) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);

        SysDept byId =  super.findById(did);//baseMapper.selectByKey(did);

        session.close();

        return byId;
    }

    @Override
    public List<SysDept> queryList(SysDept dept) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);

        //2.1 创建 example 对象
        Example<SysDept> example = new Example<SysDept>();

        //2.2 创建 动态拼接Sql对象
        Example.Criteria<SysDept> criteria = example.createCriteriaSelective();

        if(dept!=null)
        {
            criteria.andLike(SysDept::getDeptName, StringLike.strCaonstLike(dept.getDeptName()));

            criteria.andEqualTo(SysDept::getStatus, dept.getStatus());


        }
        //2.3 查询
        List<SysDept> sysDepts = baseMapper.selectByExample(example);

        session.close();


        return sysDepts;
    }

    @Override
    public PageInfo<SysDept> queryListByPagte(SysDept dept, int currentPage, int size) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(SysDeptMapper.class);

        //1. 查询前构建分页对象

        PageHelper.startPage(currentPage, size);

        //2. 构建查询参数

        //2.1 创建 example 对象
        Example<SysDept> example = new Example<SysDept>();

        //2.2 创建 动态拼接Sql对象
        Example.Criteria<SysDept> criteria = example.createCriteriaSelective();

        if(dept!=null)
        {
            criteria.andLike(SysDept::getDeptName, StringLike.strCaonstLike(dept.getDeptName()));

            criteria.andEqualTo(SysDept::getStatus, dept.getStatus());
            criteria.andEqualTo(SysDept::getParentId, dept.getParentId());

        }
        //2.3 查询
        List<SysDept> sysDepts = baseMapper.selectByExample(example);
        //2.4 封装成pageInfo 对象
        PageInfo<SysDept> pageInfo = new PageInfo<>(sysDepts);

        session.close();
        return pageInfo;
    }

    /**
     * 修改子元素关系
     *
     * @param deptId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateDeptChildren(String deptId, String newAncestors, String oldAncestors)
    {
        List<SysDept> children = baseMapper.selectByFieldList(SysDept::getParentId, Arrays.asList(deptId));
        for (SysDept child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseMapper.updateDeptChildren(children);
        }
    }
}
